package paper.batch;

import java.security.MessageDigest;
import java.security.DigestInputStream;

public class MD5 {
    MessageDigest md = null;

    public MD5() {
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.err.println("Md error " + e);
        }
    }

    public void update(byte[] buffer, int len) {
        md.update(buffer, 0, len);
    }

    public void update(byte[] buffer, int start, int len) {
        md.update(buffer, start, len);
    }

    public String digest() {
        byte[] digest = md.digest();
        StringBuilder uniqueName = new StringBuilder(32);
        for (int i = 0; i < digest.length; i++) {
            String hexVal = Integer.toHexString(digest[i] & 255);
            uniqueName.append(hexVal.length() < 2 ? "0" + hexVal : hexVal);
        }
        return uniqueName.toString();
    }
}
